//
//  TKYDateIntervalController.m
//  StationHouseHelper
//
//  Created by apple on 2018/7/16.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYDateIntervalController.h"
#import "TKYProgressDateController.h"
@interface TKYDateIntervalController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)BaseTabelView *tabelView;
@property (nonatomic, strong)NSMutableArray *dataSourceArr;

@property (assign, nonatomic) NSIndexPath *selIndex;

/** 选择的时间 */
@property (nonatomic, strong)NSString *selectedDateStr;


@end

@implementation TKYDateIntervalController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUPUI];
}

#pragma mark ---UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }
    if (_selIndex == indexPath) {
         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
       cell.accessoryType = UITableViewCellAccessoryNone;
    }
    cell.textLabel.text = self.dataSourceArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell  = [tableView cellForRowAtIndexPath:_selIndex];
     cell.accessoryType = UITableViewCellAccessoryNone;
    NSString * ss = self.dataSourceArr[indexPath.row];
    self.selectedDateStr = ss;
    //记录当前的选择的位置
    _selIndex = indexPath;
    NSLog(@"%@", ss);
    //当前选择的打钩
    UITableViewCell *cell2  = [tableView cellForRowAtIndexPath:indexPath];
    cell2.accessoryType = UITableViewCellAccessoryCheckmark;
}

- (BaseTabelView *)tabelView{
    if (_tabelView == nil) {
        _tabelView = [[BaseTabelView alloc]initWithFrame:CGRectMake(0, MyNavBarHeight, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
        _tabelView.delegate = self;
        _tabelView.dataSource = self;
        _tabelView.tableHeaderView = [[UIView alloc]init];
    }
    return _tabelView;
}

- (NSMutableArray *)dataSourceArr{
    if (_dataSourceArr == nil) {
        _dataSourceArr = [NSMutableArray arrayWithObjects:@"1分钟",@"5分钟",@"10分钟",@"15分钟", nil];
    }
    return _dataSourceArr;
}

- (void)setUPUI{
    self.navigationItem.title = @"间隔时间选择";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tabelView];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveBarButtonItemAction:)];
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back_btn"] style:UIBarButtonItemStylePlain target:self action:@selector(backBarButtonItemAction:)];
    
}

//返回
- (void)backBarButtonItemAction:(UIBarButtonItem *)barButtonItem{
    [self.navigationController popViewControllerAnimated:YES];
}

//保存
- (void)saveBarButtonItemAction:(UIBarButtonItem *)barButtonItem{
    //保存要间隔的时间
    [[NSUserDefaults standardUserDefaults] setObject:self.selectedDateStr forKey:@"selectedDateStr"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    //返回设置界面
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
